package com.homework.reflect.test;
import org.junit.Test;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
/**
 * @author nmm
 * @date 2024-03-2024/3/25
 * @deriction 通过class实例对其方法做具体操作(调用相关method方法API)
 */
public class StudentReflectTest3 {

    @Test
    public void testclazzmethod() throws Exception {
    Class StuClass = Class.forName("com.homework.reflect.bean.Student"); //获取Class对象

        Method []publicmethods = StuClass.getMethods();//获取公开方法
        System.out.println("公开==========");
        for (int i = 0; i < publicmethods.length; i++) {
//            System.out.println( publicmethods[i].getName());
        }


        System.out.println("所有===========");
        Method[] declaredMethods = StuClass.getDeclaredMethods();
        for (int i = 0; i < declaredMethods.length; i++) {
            System.out.println(declaredMethods[i].getName());
//            System.out.println(declaredMethods[i].getReturnType());//获取方法返回值类型(全限定类名)
//            System.out.println(declaredMethods[i].getReturnType().getSimpleName());//获取方法返回值类型简单类型名

//            Class<?>[] paraTypes = declaredMethods[i].getParameterTypes();//获取方法参数类型
//            System.out.println("参数个数:"+paraTypes.length);
//            for (Class<?> paraType : paraTypes) {
//                System.out.println(paraType.getSimpleName());
//            }
        }


        Constructor constructor = StuClass.getConstructor(String.class, int.class, String.class);
        Object potato = constructor.newInstance("土豆🥔", 18, "男");
        System.out.println(potato);

        Method talkMthod = StuClass.getDeclaredMethod("talkAbout", String.class, String.class, String.class);
        talkMthod.setAccessible(true);
        talkMthod.invoke(potato,"nmm豆","你妈妈potato","java");


        Method mystudyMethod = StuClass.getDeclaredMethod("mystudy", String.class, long.class);
        mystudyMethod.setAccessible(true);
        mystudyMethod.invoke(potato,"土damn",1l);

        System.out.println(StuClass.getPackage().getName());//获取当前class实例所在的全限定包名
    }
}